//
//  ConnectModel.m
//  项目
//
//  Created by 焦钟培 on 14-12-29.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ConnectModel.h"
#import "Reachability.h"
@implementation ConnectModel
- (void)dealloc
{
    [_data release];
    [super dealloc];
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.data = [NSMutableData data];
    }
    return self;
}
+ (NSString *)connectWithUrlStr: (NSString *)str parmaters: (NSMutableArray *)parmaters
{
    NSString *myStr = @"";
    for (int i = 0; i < [parmaters count]; i++) {
        NSDictionary *dic = [parmaters objectAtIndex:i];
        if ([myStr length] == 0) {
            myStr = [NSString stringWithFormat:@"%@?%@=%@", myStr, [dic.allKeys firstObject], [dic objectForKey:[dic.allKeys firstObject]]];
        }else{
            myStr = [NSString stringWithFormat:@"%@&%@=%@", myStr, [dic.allKeys firstObject], [dic objectForKey:[dic.allKeys firstObject]]];
        }
    }
    str = [NSString stringWithFormat:@"%@%@", str,myStr ];
    return str;
}
- (void)startConnectWithUrlStr: (NSString *)str parmaters: (NSMutableArray *)parmaters
{
    str = [NSString stringWithFormat:@"%@",[ConnectModel connectWithUrlStr:str parmaters:parmaters]];
    NSLog(@"%@",str);
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *quest = [NSMutableURLRequest requestWithURL:url];
    [quest setHTTPMethod:@"GET"];
    [NSURLConnection connectionWithRequest:quest delegate:self];
}
+ (void)connectModelWithUrlStr: (NSString *)str parmaters: (NSMutableArray *)parmaters block:(ConnectBlock)block
{
    ConnectModel *model = [[ConnectModel alloc] init];
    model.myBlock = block;
    [model startConnectWithUrlStr:str parmaters:parmaters];
    [model release];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [_data appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    self.myBlock (_data);
}




@end
